package com.stylefeng.guns.modular.personMessage.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.modular.system.model.CarMa;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.system.model.PersonMessage;
import com.stylefeng.guns.modular.personMessage.service.IPersonMessageService;

import java.text.SimpleDateFormat;

/**
 * 人员管理控制器
 *
 * @author fengshuonan
 * @Date 2018-09-12 13:44:14
 */
@Controller
@RequestMapping("/personMessage")
public class PersonMessageController extends BaseController {

    private String PREFIX = "/personMessage/personMessage/";

    @Autowired
    private IPersonMessageService personMessageService;

    /**
     * 跳转到人员管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "personMessage.html";
    }

    /**
     * 跳转到添加人员管理
     */
    @RequestMapping("/personMessage_add")
    public String personMessageAdd() {
        return PREFIX + "personMessage_add.html";
    }

    /**
     * 跳转到修改人员管理
     */
    @RequestMapping("/personMessage_update/{personMessageId}")
    public String personMessageUpdate(@PathVariable Integer personMessageId, Model model) {
        PersonMessage personMessage = personMessageService.selectById(personMessageId);
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time=format.format(personMessage.getDatetime());
        model.addAttribute("time",time);
        model.addAttribute("item",personMessage);
        LogObjectHolder.me().set(personMessage);
        return PREFIX + "personMessage_edit.html";
    }

    /**
     * 获取人员管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        EntityWrapper<PersonMessage> carMaWrapper =new EntityWrapper<PersonMessage>();
        carMaWrapper.like("name",condition);
        return personMessageService.selectList(carMaWrapper);
    }

    /**
     * 新增人员管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(PersonMessage personMessage) {
        personMessageService.insert(personMessage);
        return SUCCESS_TIP;
    }

    /**
     * 删除人员管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer personMessageId) {
        personMessageService.deleteById(personMessageId);
        return SUCCESS_TIP;
    }

    /**
     * 修改人员管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(PersonMessage personMessage) {
        personMessageService.updateById(personMessage);

        return SUCCESS_TIP;
    }

    /**
     * 人员管理详情
     */
    @RequestMapping(value = "/detail/{personMessageId}")
    @ResponseBody
    public Object detail(@PathVariable("personMessageId") Integer personMessageId) {
        return personMessageService.selectById(personMessageId);
    }
}
